package com.base.common.service;

import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.common.persistence.BaseMapper;
import com.base.common.persistence.Pagination;

/**
 * <p>
 * IService 实现类（ 泛型：M 是 mapper 对象，T 是实体 ， PK 是主键泛型 ）
 * </p>
 * @author tcq
 * @author v 1.0
 * @date 2019年1月30日 下午2:06:01
 */
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {

	@Autowired
    protected M baseMapper;
	
	@Override
	public T findById(Serializable id) {
		return baseMapper.findById(id);
	}

	@Override
	public T findOne(T entity) {
		List<T> list = findByEntity(entity);
		return (list == null || list.isEmpty())? null : list.get(0);
	}
	
	@Override
	public List<T> findByEntity(T entity) {
		return baseMapper.findByEntity(entity);
	}

	@Override
	public List<T> findByPage(T entity, Pagination page) {
		return baseMapper.findByEntityWithPage(entity, page);
	}
	
	
	@Override
	public void insert(T entity) {
		baseMapper.insert(entity);
	}

	@Override
	public void deleteById(Serializable id) {
		baseMapper.delete(id);
	}

	@Override
	public void updateById(T entity) {
		baseMapper.update(entity);
	}

	@Override
	public void updateByEntity(T entity) {
		baseMapper.updateIfPossible(entity);
	}

}
